package com.controller;



import com.dao.Maintenance_orderDao;
import com.domain.Maintenance_order;
import com.domain.Maintenance_process;
import com.tool.Until;
import com.webservice.ProcessWebservice;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Component
public class ListenerController {



    @Autowired
    private Maintenance_orderDao maintenance_orderDao;

    @Resource
    private ProcessWebservice processWebservice;


    /**
     * 过保修期结束追评
     */
    //0/5 * * * * ?  每5秒执行一次
    @Scheduled(cron = "0 0 1 * * *") // 每天凌晨1点执行一次
    public void completeMaintenance() {
        //输出时间测试
        Date now = new Date(System.currentTimeMillis());
        Integer warrantyPeriod = 0;

        List<Maintenance_order> maintenance_orderList = maintenance_orderDao.findByState(9);
        if (maintenance_orderList != null){
            for (Maintenance_order maintenance_order:maintenance_orderList){
                List<Maintenance_process> processList = maintenance_orderDao.findOrderProcess(maintenance_order.getId());
                warrantyPeriod = maintenance_order.getWarranty_period();
                Date finishedTime = new Date();
                for (Maintenance_process maintenance_process:processList){
                    if (maintenance_process.getTask_id() == 7){
                        finishedTime = maintenance_process.getFinished_time();
                    }
                }
                long day=0;
                day=(now.getTime()-finishedTime.getTime())/(24*60*60*1000);

                if (warrantyPeriod != null){
                    if ( day > (int)warrantyPeriod){
                        processWebservice.stop(maintenance_order.getId());
                        //System.out.println("已完成");
                    }
                }


            }
        }
    }

//    /**
//     * 清除无效维修单
//     */
//    @Scheduled(cron = "0 0 1 * * *") // 每天凌晨1点执行一次
//    public void delInvalidityOrder(){
//        List<Maintenance_order> orders = maintenance_orderDao.findAllOrder();
//        if (orders!=null){
//            for (Maintenance_order order:orders){
//                if (order.getOrder_state()==8
//                        || order.getOrder_state()==9
//                        || order.getOrder_state()==10
//                        || order.getOrder_state()==11
//                        || order.getOrder_state()==15){
//                    continue;
//                }
//                Task task = Until.findTaskById(order.getId());
//                if (task == null){
//                    Until.delOrder(order.getId());
//                }
//                if (maintenance_orderDao.findOrderProcess(order.getId()) == null){
//                    Until.delOrder(order.getId());
//                }
//            }
//        }
//    }


    /**
     * 清除过期维修单
     */
//    @Scheduled(cron = "0 0 1 * * *") // 每天凌晨1点执行一次
//    public void delOverdueOrder(){
//        List<Maintenance_order> orders = maintenance_orderDao.findAllOrder();
//        if (orders!=null){
//            for (Maintenance_order order:orders){
//                if (order.getOrder_state() != 8 && order.getOrder_state() != 9 &&
//                        order.getOrder_state() != 10 && order.getOrder_state() != 11
//                        && order.getOrder_state() != 15){
//                    order = Until.setOrder(order);
//                    Date now = new Date(System.currentTimeMillis());
//                    if(order.getOrderPeriodOfValidity() != 0){
//                        long day = (now.getTime()-order.getCreateTime().getTime())/(24*60*60*1000);
//                        if (day > order.getOrderPeriodOfValidity()){
//                            processWebservice.stopOrder(order.getId(), 1, "维修单超时");
//                        }
//                    }
//                }else if (order.getOrder_state() == 8){
//                    order = Until.setOrder(order);
//                    Date now = new Date(System.currentTimeMillis());
//                    if(order.getOrderPeriodOfValidity() != 0){
//                        long day = (now.getTime()-order.getCreateTime().getTime())/(24*60*60*1000);
//                        if (day > order.getOrderPeriodOfValidity()){
//                            processWebservice.evaluation(order.getId(), String.valueOf(5), String.valueOf(5),String.valueOf(5), "默认好评");
//                        }
//                    }
//                }
//
//
//            }
//        }
//    }

    /**
     * 审批超时退回审核员
     */
    /*@Scheduled(cron = "0 0 1 * * *") // 每天凌晨1点执行一次
    public void backToApprover(){
        List<Maintenance_order> orders = maintenance_orderDao.findAllOrder();
        Date now = new Date(System.currentTimeMillis());
        if (orders!=null){
            for (Maintenance_order order:orders){
                if (order.getOrder_state() == 4 || order.getOrder_state() == 7 || order.getOrder_state() == 12){
                    List<Maintenance_process> processList = maintenance_orderDao.findOrderProcess(order.getId());
                    if (processList!=null && processList.size()>0){
                        for (int i = processList.size() - 1;i>=0;i--){
                            if (processList.get(i).getTask_id() == 2){
                                long day = (now.getTime()-processList.get(i).getFinished_time().getTime())/(24*60*60*1000);
                                if(day > 2){
                                    Task task = Until.findTaskById(order.getId());
                                    Maintenance_process process = new Maintenance_process();
                                    process.setTask_id(4);
                                    process.setFinished_time(new Date(System.currentTimeMillis()));
                                    process.setTransactor_id(1);
                                    process.setDetail("报修审批超时");
                                    process.setReason("超时");
                                    process.setOrder_id(order.getId());
                                    Until.skipToTargetNode(task, "usertask2", processList.get(i).getTransactor_id().toString());
                                    order.setOrder_state(1);
                                    maintenance_orderDao.addProcess(process);
                                    maintenance_orderDao.updateOrder(order);
                                }
                                break;
                            }
                            else if (processList.get(i).getTask_id() == 3){
                                long day = (now.getTime()-processList.get(i).getFinished_time().getTime())/(24*60*60*1000);
                                if(day > 2){
                                    Task task = Until.findTaskById(order.getId());
                                    Maintenance_process process = new Maintenance_process();
                                    process.setTask_id(11);
                                    process.setFinished_time(new Date(System.currentTimeMillis()));
                                    process.setTransactor_id(1);
                                    process.setDetail("竞单审批超时");
                                    process.setReason("超时");
                                    process.setOrder_id(order.getId());
                                    Until.skipToTargetNode(task, "usertask4", processList.get(i).getTransactor_id().toString());
                                    order.setOrder_state(2);
                                    maintenance_orderDao.addProcess(process);
                                    maintenance_orderDao.updateOrder(order);
                                }
                                break;
                            }
                            else if (processList.get(i).getTask_id() == 7){
                                long day = (now.getTime()-processList.get(i).getFinished_time().getTime())/(24*60*60*1000);
                                if(day > 2){
                                    Task task = Until.findTaskById(order.getId());
                                    Maintenance_process process = new Maintenance_process();
                                    process.setTask_id(8);
                                    process.setFinished_time(new Date(System.currentTimeMillis()));
                                    process.setTransactor_id(1);
                                    process.setDetail("验收审批超时");
                                    process.setReason("超时");
                                    process.setOrder_id(order.getId());
                                    Until.skipToTargetNode(task, "usertask13", processList.get(i).getTransactor_id().toString());
                                    order.setOrder_state(6);
                                    maintenance_orderDao.addProcess(process);
                                    maintenance_orderDao.updateOrder(order);
                                }
                                break;
                            }
                            else if (processList.get(i).getTask_id() == 13){
                                long day = (now.getTime()-processList.get(i).getFinished_time().getTime())/(24*60*60*1000);
                                if(day > 2){
                                    Task task = Until.findTaskById(order.getId());
                                    Maintenance_process process = new Maintenance_process();
                                    process.setTask_id(11);
                                    process.setFinished_time(new Date(System.currentTimeMillis()));
                                    process.setTransactor_id(1);
                                    process.setDetail("竞单审批超时");
                                    process.setReason("超时");
                                    process.setOrder_id(order.getId());
                                    Until.skipToTargetNode(task, "usertask7", processList.get(i).getTransactor_id().toString());
                                    order.setOrder_state(13);
                                    maintenance_orderDao.addProcess(process);
                                    maintenance_orderDao.updateOrder(order);
                                }
                                break;
                            }
                        }
                    }
                }

            }
        }
    }*/


}
